<script setup lang="ts">
import {useRoute} from "vue-router";
import {onMounted, ref} from "vue";
import {fetchSearch} from "../request/req.ts";
import {BACKEND_URL} from "../request/Api.ts";

const route = useRoute()

const orderInfo = ref<any>({
  TypeNames: '',
  CartTotal: null,
  OrdersNumber: null,
  OrdersTime: '',
  BuyerName: '',
  BuyerPhone: '',
  Address: '',
  ShipmentState: '',
  ListCart: [
    {
      Id: null,
      CartOrders: '',
      GoodsId: null,
      BuyQty: null,
      GoodsName: '',
      Pirce: null,
      PicUrls: '',
      Total: null
    }
  ],
})

/**
 * 获取订单信息
 */
const getOrderInfo = async () => {
  // 从路由参数获取订单信息
  const response = await fetchSearch({t: route.query.type, k: route.query.keyword})
  console.log(response)
  orderInfo.value = response
}

/**
 * 计算商品总数
 */
const getTotalQuantity = () => {
  if (!orderInfo.value.ListCart || !Array.isArray(orderInfo.value.ListCart)) return 0
  return orderInfo.value.ListCart.reduce((total: number, item: any) => total + (item.BuyQty || 0), 0)
}

onMounted(() => {
  getOrderInfo()
})
</script>

<template>
  <div class="client-order-page">
    <div class="order-container">
      <!-- 订单基本信息 -->
      <div class="section order-info-section">
        <h2>订单基本信息</h2>
        <div class="info-grid">
          <div class="info-item">
            <span class="label">订单编号</span>
            <span class="value">{{ orderInfo.OrdersNumber || '暂无' }}</span>
          </div>
          <div class="info-item">
            <span class="label">下单时间</span>
            <span class="value">{{ orderInfo.OrdersTime || '暂无' }}</span>
          </div>
          <div class="info-item">
            <span class="label">收货人</span>
            <span class="value">{{ orderInfo.BuyerName || '暂无' }}</span>
          </div>
          <div class="info-item">
            <span class="label">收货电话</span>
            <span class="value">{{ orderInfo.BuyerPhone || '暂无' }}</span>
          </div>
          <div class="info-item full-width">
            <span class="label">收货地址</span>
            <span class="value">{{ orderInfo.Address || '暂无' }}</span>
          </div>
          <div class="info-item">
            <span class="label">订单状态</span>
            <span class="value status-badge">
              {{ orderInfo.ShipmentState || '暂无' }}
            </span>
          </div>
          <div class="info-item">
            <span class="label">订单总价</span>
            <span class="value total-price">HK${{ orderInfo.CartTotal || 0 }}</span>
          </div>
        </div>
      </div>

      <!-- 订单商品详情 -->
      <div class="section order-details-section">
        <h2>订单商品详情</h2>
        <div class="order-items">
          <div
              v-for="item in orderInfo.ListCart"
              :key="item.Id"
              class="order-item"
          >
            <div class="item-image">
              <img :src="BACKEND_URL + item.PicUrls" :alt="item.GoodsName">
            </div>
            <div class="item-info">
              <h3 class="item-name">{{ item.GoodsName }}</h3>
              <div class="item-details">
                <div class="detail-row">
                  <span class="detail-label">单价：</span>
                  <span class="detail-value price">HK${{ item.Pirce }}</span>
                </div>
                <div class="detail-row">
                  <span class="detail-label">数量：</span>
                  <span class="detail-value">{{ item.BuyQty }}</span>
                </div>
                <div class="detail-row">
                  <span class="detail-label">小计：</span>
                  <span class="detail-value subtotal">HK${{ item.Total }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 订单汇总 -->
      <div class="section summary-section">
        <div class="summary-content">
          <div class="summary-item">
            <span class="summary-label">商品总数：</span>
            <span class="summary-value">{{ getTotalQuantity() }}</span>
          </div>
          <div class="summary-item total">
            <span class="summary-label">订单总价：</span>
            <span class="summary-value total-amount">HK${{ orderInfo.CartTotal || 0 }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.client-order-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.order-container {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

.page-header h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.section {
  padding: 30px;
  border-bottom: 1px solid #f0f0f0;
}

.section:last-child {
  border-bottom: none;
}

.section h2 {
  margin: 0 0 25px 0;
  color: #333;
  font-size: 20px;
  font-weight: 600;
  position: relative;
  padding-bottom: 12px;
}

.section h2::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 50px;
  height: 3px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  border-radius: 2px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.info-item.full-width {
  grid-column: 1 / -1;
}

.info-item .label {
  color: #666;
  font-size: 14px;
  font-weight: 500;
}

.info-item .value {
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.status-badge {
  display: inline-block;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  min-width: 80px;
}

.total-price {
  color: #e74c3c;
  font-size: 18px;
}

.order-items {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.order-item {
  display: flex;
  gap: 20px;
  padding: 20px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  background: #fafafa;
  transition: all 0.3s ease;
}

.order-item:hover {
  border-color: #667eea;
  background: #f8f9ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.1);
}

.item-image {
  flex-shrink: 0;
}

.item-image img {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 8px;
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.item-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.item-name {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
  line-height: 1.4;
}

.item-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.detail-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.detail-label {
  color: #666;
  font-size: 14px;
  min-width: 60px;
}

.detail-value {
  color: #333;
  font-weight: 500;
}

.detail-value.price {
  color: #e74c3c;
  font-weight: 600;
}

.detail-value.subtotal {
  color: #e74c3c;
  font-weight: 700;
  font-size: 16px;
}

.summary-section {
  background: linear-gradient(135deg, #f8f9ff 0%, #f1f3ff 100%);
}

.summary-content {
  max-width: 400px;
  margin-left: auto;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  font-size: 16px;
}

.summary-item.total {
  border-top: 2px solid #e0e6ff;
  margin-top: 15px;
  padding-top: 20px;
  font-size: 18px;
  font-weight: 600;
}

.summary-label {
  color: #333;
}

.summary-value {
  color: #333;
  font-weight: 600;
}

.summary-value.total-amount {
  color: #e74c3c;
  font-size: 24px;
  font-weight: 800;
}

@media (max-width: 768px) {
  .client-order-page {
    padding: 10px;
  }

  .section {
    padding: 20px;
  }

  .page-header h1 {
    font-size: 24px;
  }

  .info-grid {
    grid-template-columns: 1fr;
    gap: 15px;
  }

  .order-item {
    flex-direction: column;
    gap: 15px;
    padding: 15px;
  }

  .item-image img {
    width: 80px;
    height: 80px;
  }

  .summary-content {
    margin: 0;
  }
}

@media (max-width: 480px) {
  .section {
    padding: 15px;
  }

  .page-header h1 {
    font-size: 20px;
  }

  .section h2 {
    font-size: 18px;
  }
}
</style>
